package com.hzeport.bgs22emduty.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzeport.bgs22emduty.service.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

@Service
@ConditionalOnProperty(prefix = "system-config", name = "cacheImpl", havingValue = "redis")
@Slf4j
public class RedisCacheServiceImpl implements CacheService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public <T> T getSave(String key, Supplier<T> supplier, Integer expireTime, Class<T> clazz) {
        if (key == null || supplier == null || expireTime == null || clazz == null) {
            log.info("调用getSave参数为空");
            return null;
        }
        try {
            String value = redisTemplate.opsForValue().get(key);
            if (value == null) {
                T result = supplier.get();
                String jsonString = JSONObject.toJSONString(result);
                redisTemplate.opsForValue().set(key, jsonString, expireTime, TimeUnit.SECONDS);
                log.info("未查询到key：" + key + "的缓存，将supplier存入缓存并返回");
                return result;
            } else {
                log.info("查询到key：" + key + "的缓存");
                return JSONObject.parseObject(value, clazz);
            }
        } catch (Exception e) {
            log.error("getSave获取缓存异常：{}",e.getMessage());
            return null;
        }
    }

    @Override
    public <T> List<T> getSaveList(String key, Supplier<List<T>> supplier, Integer expireTime, Class<T> clazz) {
        if (key == null || supplier == null || expireTime == null || clazz == null) {
            log.info("调用getSaveList参数为空");
            return null;
        }
        try {
            String value = redisTemplate.opsForValue().get(key);
            if (value == null) {
                List<T> result = supplier.get();
                String jsonString = JSONObject.toJSONString(result);
                redisTemplate.opsForValue().set(key, jsonString, expireTime, TimeUnit.SECONDS);
                log.info("未查询到key：" + key + "的缓存，将supplier存入缓存并返回");
                return result;
            } else {
                log.info("查询到key：" + key + "的缓存");
                return JSONArray.parseArray(value, clazz);
            }
        } catch (Exception e) {
            log.error("getSaveList获取缓存异常：{}",e.getMessage());
            return null;
        }
    }

    @Override
    public <T> T get(String key, Class<T> clazz) {
        try {
            log.info("查询key：" + key + "的缓存");
            return key == null ? null : JSONObject.parseObject(redisTemplate.opsForValue().get(key), clazz);
        } catch (Exception e) {
            log.error("get获取缓存异常：{}",e.getMessage());
            return null;
        }
    }

    @Override
    public boolean set(String key, Object value) {
        if (key == null || value == null) {
            return false;
        }
        try {
            String jsonString = JSONObject.toJSONString(value);
            redisTemplate.opsForValue().set(key, jsonString);
            log.info("key：" + key + "存入缓存");
            return true;
        } catch (Exception e) {
            log.error("存入缓存异常{}",e.getMessage());
            return false;
        }
    }

    @Override
    public boolean set(String key, Object value, Integer expireTime) {
        if (key == null || value == null) {
            return false;
        }
        try {
            if (expireTime > 0) {
                String jsonString = JSONObject.toJSONString(value);
                redisTemplate.opsForValue().set(key, jsonString, expireTime, TimeUnit.SECONDS);
                log.info("key：" + key + "存入缓存");
            } else {
                return set(key, value);
            }
            return true;
        } catch (Exception e) {
            log.error("存入缓存异常{}",e.getMessage());
            return false;
        }
    }

    @Override
    public void del(String key) {
        if (key != null) {
            redisTemplate.delete(key);
            log.info("删除key：" + key);
        }
    }

    @Override
    public boolean del(List<String> keys) {
        if (keys.size() > 0) {
            log.info("删除key：" + String.join(",", keys));
            return Optional.ofNullable(redisTemplate.delete(keys)).orElse(0L) > 0;
        } else {
            return false;
        }
    }
}
